package com.code.monitor.model;

import io.netty.channel.socket.SocketChannel;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author ggh
 * @date 2023/4/15
 */
final public class CacheManager {

    private CacheManager() {
    }

    public static void set(String key, Object value) {
        CacheManagerHolder.cache.put(key, value);
    }

    public static Object get(String key) {
        return CacheManagerHolder.cache.get(key);
    }

    public static void setWebSubscriberList(SocketChannel socketChannel) {
        List<SocketChannel> socketChannelList = CacheManagerHolder.webSubsriberList;
        socketChannelList.add(socketChannel);
    }

    public static void addWebSubscriber(SocketChannel socketChannel) {
        CacheManagerHolder.webSubsriberList.add(socketChannel);
    }

    public static List<SocketChannel> getWebSubscriberList() {
        return CacheManagerHolder.webSubsriberList;
    }

    public static boolean existWebSubscriberKey(SocketChannel socketChannel) {
        return CacheManagerHolder.webSubsriberList.contains(socketChannel);
    }

    public static Collection<ServiceInfo> getServiceInfoList() {
        return CacheManagerHolder.ServiceInfoCache.values();
    }

    public static void setServiceInfo(SocketChannel socketChannel, ServiceInfo serviceInfo) {
        CacheManagerHolder.ServiceInfoCache.put(socketChannel, serviceInfo);
    }

    public static ServiceInfo getServiceInfo(String ip) {
        return CacheManagerHolder.ServiceInfoCache.get(ip);
    }

    public static ServiceInfo getServiceInfo(SocketChannel socketChannel) {
        return CacheManagerHolder.ServiceInfoCache.get(socketChannel);
    }

    public static ServiceInfo delServiceInfo(SocketChannel socketChannel) {
        return CacheManagerHolder.ServiceInfoCache.remove(socketChannel);
    }

    public static boolean existKey(SocketChannel socketChannel) {
        return CacheManagerHolder.ServiceInfoCache.containsKey(socketChannel);
    }

    public static Collection<MachineInfo> getMachineInfoList() {
        return CacheManagerHolder.machineInfoList.values();
    }

    public static MachineInfo getMachineInfo(String ip) {
        return CacheManagerHolder.machineInfoList.get(ip);
    }

    public static void addMachineInfo(MachineInfo machineInfo) {
        CacheManagerHolder.machineInfoList.put(machineInfo.getIp(), machineInfo);
    }

    public static void delMachineInfo(String ip) {
        CacheManagerHolder.machineInfoList.remove(ip);
    }

    public static List<ActivityInfo> getActivityList() {
        return CacheManagerHolder.activityInfoList;
    }

    public static void addActivityInfo(ActivityInfo activityInfo) {
        CacheManagerHolder.activityInfoList.add(activityInfo);
    }

    public static Collection<PackageInfo> getPackageInfoList() {
        return CacheManagerHolder.packageInfoList.values();
    }

    public static PackageInfo getPackageInfo(String uuid) {
        return CacheManagerHolder.packageInfoList.get(uuid);
    }

    public static void addPackageInfo(PackageInfo packageInfo) {
        CacheManagerHolder.packageInfoList.put(packageInfo.getUuid(), packageInfo);
    }

    public static void delPackageInfo(String uuid) {
        CacheManagerHolder.packageInfoList.remove(uuid);
    }

    private static class CacheManagerHolder {
        private static ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();
        private static CopyOnWriteArrayList<SocketChannel> webSubsriberList = new CopyOnWriteArrayList<>();
        private static ConcurrentHashMap<SocketChannel, ServiceInfo> ServiceInfoCache = new ConcurrentHashMap<>();
        private static ConcurrentHashMap<String, MachineInfo> machineInfoList = new ConcurrentHashMap<>();
        private static CopyOnWriteArrayList<ActivityInfo> activityInfoList = new CopyOnWriteArrayList<>();
        private static ConcurrentHashMap<String, PackageInfo> packageInfoList = new ConcurrentHashMap<>();
    }
}
